<?php
include '../controller/Conexao.php';
include '../model/Cliente.php';

class ClienteDAO{
	
	public function Insere(cliente $cliente){
		try{
			mysql_query(  "INSERT INTO cliente( nome
											  , razaosocial
											  , telefone
											  , endereco
					 						  , contato
											  , logonome)
									  VALUES ('".$cliente->getNome()."'
									  		 ,'".$cliente->getRazaosocial()."'
									  		 ,'".$cliente->getTelefone()."'
									  		 ,'".$cliente->getEndereco()."'
									  		 ,'".$cliente->getContato()."'
									  		 ,'".$cliente->getLogoNome()."')" ) or die (mysql_error());
	
		//header("location:../view/ListaDevices.php");
		}catch ( Exception $ex ){
			echo "Erro: ".$ex->getMessage();
		}
	}
	
	public function Remove($id){
		try{
			mysql_query("delete from cliente where idcliente=$id") or die (mysql_error());
			//header("location:../view/ListaDevices.php");
		}catch ( Exception $ex ){
			echo "Erro: ".$ex->getMessage();
		}
	}
	
	public function Altera(Cliente $cliente){
		try{
			mysql_query(  "UPDATE cliente SET   nome 			= 	'".$cliente->getNome()."'
											  , razaosocial 	= 	'".$cliente->getRazaosocial()."'
		 								      , telefone 		= 	'".$cliente->getTelefone()."'
											  , endereco		= 	'".$cliente->getEndereco()."'
											  , contato			= 	'".$cliente->getContato()."'
											  , logonome 		= 	'".$cliente->getLogoNome()."'
									   WHERE idcliente = ".$cliente->getIdCliente() ) or die (mysql_error());
		}catch ( Exception $ex ){
			echo "Erro: ".$ex->getMessage();
		}
							
		//header("location:../view/ListaDevices.php");
	}
	public static function GetCliente($idcliente){
		$result = mysql_query( 'select * from cliente where idcliente='.$idcliente) or die(mysqli_error()) ;
		$row = mysql_fetch_assoc($result);
			
		$cliente = new Cliente();
		$cliente->setIdCliente($row["idcliente"]);
		$cliente->setEndereco($row["endereco"]);
		$cliente->setNome($row["nome"]);
		$cliente->setRazaosocial($row["razaosocial"]);
		$cliente->setContato($row["contato"]);
		$cliente->setTelefone($row["telefone"]);
		$cliente->setLogoNome($row["logonome"]);
			
		return $cliente;
	}
	public static function Lista(){
		try{
				
			$rs = mysql_query( 'select * from cliente') or die(mysqli_error()) ;
			$result = array();
			while($row = mysql_fetch_object($rs)){
				array_push($result, $row);
			}	
			return $result;
		}catch ( Exception $ex ){
			echo "Erro: ".$ex->getMessage();
		}
	}
	
}	
	
?>
